package com.fyb.basic.juc.completableFuture;

import java.util.concurrent.CompletableFuture;

/**
 * @Author：MR.FANG
 * @name：ApplyToEither
 * 哪个任务先来就先执行那个 applyToEither
 * @Date：2025/7/2 下午2:12
 */
public class ApplyToEither {
    public static void main(String[] args) {
        SmallTool.printTimeAndTread("张三走出餐厅 来到公交站");
        SmallTool.printTimeAndTread("等待700路 或者 800路，公交到来 谁先来 就做那路公交");

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndTread("700路公交正在赶来");
            SmallTool.sleepMillis(100);
            return "700";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndTread("800路公交正在赶来");
            SmallTool.sleepMillis(300);
            return "800";
        }), firstBus -> firstBus);
        SmallTool.printTimeAndTread(String.format("%s 小白坐车回家", completableFuture.join()));
    }
}
